package com.example.kliven.because.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;
import com.example.kliven.because.R;
import com.example.kliven.because.activity.MostTopicActivity;
import com.example.kliven.because.bean.ShareBean;
import com.example.kliven.because.imagecache.DoubleCache;

import java.util.List;

import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

/**
 * Created by Kliven on 16/1/11.
 */
public class HomeShareLVAdapter extends BaseAdapter implements View.OnClickListener {

    private static final int TYPE_HEAD = 0;
    private static final int TYPE_TOPINFO = 1;
    private static final int TYPE_USER_INFO = 2;
    private Context context;
    private LayoutInflater inflater;
    private List<ShareBean> shareDatas;

    private RequestQueue queue;

    public HomeShareLVAdapter(Context context, List<ShareBean> shareDatas) {
        this.context = context;
        this.shareDatas = shareDatas;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return shareDatas.size();
    }

    @Override
    public Object getItem(int i) {
        return shareDatas.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return TYPE_HEAD;
        }
        if (position == 1) {
            return TYPE_TOPINFO;
        }
        {
            return TYPE_USER_INFO;
        }
    }

    @Override
    public int getViewTypeCount() {
        return 3;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {

        ViewHodler_head hodler_head = null;
        ViewHodler_topInfo hodler_topInfo = null;
        ViewHodler_userInfo hodler_userInfo = null;


        int type = getItemViewType(i);

        if (view == null) {
            switch (type) {

                case TYPE_HEAD:
                    hodler_head = new ViewHodler_head();
                    view = inflater.inflate(R.layout.item_share_type1, viewGroup, false);

                    hodler_head.tv_top1 = (TextView) view.findViewById(R.id.tv_top1);
                    hodler_head.tv_top_shareCount1 = (TextView) view.findViewById(R.id.tv_top_shareCount1);
                    hodler_head.tv_top2 = (TextView) view.findViewById(R.id.tv_top2);
                    hodler_head.tv_top_shareCount2 = (TextView) view.findViewById(R.id.tv_top_shareCount2);
                    TextView btn = (TextView) view.findViewById(R.id.btn_top3);
                    btn.setFocusable(false);
                    btn.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = new Intent(context,MostTopicActivity.class);
                            context.startActivity(intent);
                        }
                    });
                    view.setTag(hodler_head);
                    break;
                case TYPE_TOPINFO:

                    hodler_topInfo = new ViewHodler_topInfo();
                    view = inflater.inflate(R.layout.item_share_type2, viewGroup, false);

                    hodler_topInfo.net_workImage1 = (NetworkImageView) view.findViewById(R.id.net_workImage1);
                    hodler_topInfo.net_workImage2 = (NetworkImageView) view.findViewById(R.id.net_workImage2);
                    hodler_topInfo.net_workImage3 = (NetworkImageView) view.findViewById(R.id.net_workImage3);
                    hodler_topInfo.net_workImage4 = (NetworkImageView) view.findViewById(R.id.net_workImage4);
                    hodler_topInfo.net_workImage5 = (NetworkImageView) view.findViewById(R.id.net_workImage5);
                    hodler_topInfo.net_workImage6 = (NetworkImageView) view.findViewById(R.id.net_workImage6);
                    hodler_topInfo.net_workImage7 = (NetworkImageView) view.findViewById(R.id.net_workImage7);
                    hodler_topInfo.net_workImage8 = (NetworkImageView) view.findViewById(R.id.net_workImage8);
                    hodler_topInfo.net_workImage9 = (NetworkImageView) view.findViewById(R.id.net_workImage9);
                    hodler_topInfo.net_workImage10 = (NetworkImageView) view.findViewById(R.id.net_workImage10);

                    view.setTag(hodler_topInfo);
                    break;

                case TYPE_USER_INFO:
                    hodler_userInfo = new ViewHodler_userInfo();
                    view = inflater.inflate(R.layout.item_share_type3, viewGroup, false);

                    hodler_userInfo.tv_username = (TextView) view.findViewById(R.id.tv_username);
                    hodler_userInfo.tv_commend = (TextView) view.findViewById(R.id.tv_commend);
                    hodler_userInfo.tv_food_name = (TextView) view.findViewById(R.id.tv_food_name);
                    hodler_userInfo.tv_food_location = (TextView) view.findViewById(R.id.tv_food_location);
                    hodler_userInfo.iv_user_img = (NetworkImageView) view.findViewById(R.id.iv_user_img);
                    hodler_userInfo.iv_user_impot_img = (NetworkImageView) view.findViewById(R.id.iv_user_impot_img);
                    ImageView iv_share = (ImageView) view.findViewById(R.id.iv_share);
                    iv_share.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            showShare();
                        }
                    });
                    view.setTag(hodler_userInfo);
                    break;
            }
        } else {
            switch (type) {
                case TYPE_HEAD:
                    hodler_head = (ViewHodler_head) view.getTag();
                    break;
                case TYPE_TOPINFO:
                    hodler_topInfo = (ViewHodler_topInfo) view.getTag();
                    break;

                case TYPE_USER_INFO:
                    hodler_userInfo = (ViewHodler_userInfo) view.getTag();
                    break;
            }
        }

        switch (type) {
            case TYPE_HEAD:
                hodler_head.tv_top1.setText(shareDatas.get(i).getShare_tag_name());
                hodler_head.tv_top_shareCount1.setText(shareDatas.get(i).getShare_numb());

                hodler_head.tv_top2.setText(shareDatas.get(i + 1).getShare_tag_name());
                hodler_head.tv_top_shareCount2.setText(shareDatas.get(i + 1).getShare_numb());

//                hodler_head.tv_top3.setText();
                break;
            case TYPE_TOPINFO:
                queue = Volley.newRequestQueue(context);
                // 5. 初始化缓存
                DoubleCache cache1 = new DoubleCache();
                // 4. 初始化ImageLoager
                ImageLoader imageLoader1 = new ImageLoader(queue, cache1);

                // 1. 设置默认图片;
                hodler_topInfo.net_workImage1.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage1.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage1.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage2.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage2.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage2.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage3.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage3.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage3.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage4.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage4.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage4.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage5.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage5.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage5.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage6.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage6.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage6.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage7.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage7.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage7.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage8.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage8.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage8.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage9.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage9.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage9.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                hodler_topInfo.net_workImage10.setDefaultImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage10.setErrorImageResId(R.mipmap.error_img);
                hodler_topInfo.net_workImage10.setImageUrl(shareDatas.get(i).getTop_main_photo_url(), imageLoader1);

                break;
            case TYPE_USER_INFO:
                hodler_userInfo.tv_username.setText(shareDatas.get(i).getMember_name());
                hodler_userInfo.tv_commend.setText(shareDatas.get(i).getRecommand_time());
                hodler_userInfo.tv_food_name.setText(shareDatas.get(i).getShop_name());
                hodler_userInfo.tv_food_location.setText(shareDatas.get(i).getDish_name());

                queue = Volley.newRequestQueue(context);
                // 5. 初始化缓存
                DoubleCache cache = new DoubleCache();
                // 4. 初始化ImageLoager
                ImageLoader imageLoader = new ImageLoader(queue, cache);

                // 1. 设置默认图片;
                hodler_userInfo.iv_user_img.setDefaultImageResId(R.mipmap.moren);
                hodler_userInfo.iv_user_impot_img.setDefaultImageResId(R.mipmap.error_img);
                // 2. 设置Error图片;
                hodler_userInfo.iv_user_img.setErrorImageResId(R.mipmap.moren);
                hodler_userInfo.iv_user_impot_img.setErrorImageResId(R.mipmap.error_img);
                // 3. 加载网络图片;
                hodler_userInfo.iv_user_img.setImageUrl(shareDatas.get(i).getIco(), imageLoader);
                hodler_userInfo.iv_user_impot_img.setImageUrl(shareDatas.get(i).getMain_photo_url(), imageLoader);
                break;
        }

        return view;
    }

    @Override
    public void onClick(View view) {
        Intent intent = new Intent(context, MostTopicActivity.class);
        context.startActivity(intent);
    }

    class ViewHodler_head {
        TextView tv_top1, tv_top2, tv_top3, tv_top_shareCount1, tv_top_shareCount2, tv_top_shareCount3;
    }

    class ViewHodler_topInfo {
        NetworkImageView net_workImage1, net_workImage2, net_workImage3, net_workImage4, net_workImage5, net_workImage6,
                net_workImage7, net_workImage8, net_workImage9, net_workImage10;

    }

    class ViewHodler_userInfo {
        NetworkImageView iv_user_img, iv_user_impot_img;
        TextView tv_username, tv_commend, tv_food_name, tv_food_location, tv_tag_list, tv_share_desc;
    }
    private void showShare() {
        ShareSDK.initSDK(context);
        OnekeyShare oks = new OnekeyShare();
        //关闭sso授权
        oks.disableSSOWhenAuthorize();
// 分享时Notification的图标和文字  2.5.9以后的版本不调用此方法
        //oks.setNotification(R.drawable.ic_launcher, getString(R.string.app_name));
        // title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
        oks.setTitle("标题");
        // titleUrl是标题的网络链接，仅在人人网和QQ空间使用
        oks.setTitleUrl("http://sharesdk.cn");
        // text是分享文本，所有平台都需要这个字段
        oks.setText("我是分享文本，啦啦啦~");
        // imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
        oks.setImagePath("/sdcard/test.jpg");//确保SDcard下面存在此张图片
        // url仅在微信（包括好友和朋友圈）中使用
        oks.setUrl("http://sharesdk.cn");
        // comment是我对这条分享的评论，仅在人人网和QQ空间使用
        oks.setComment("我是测试评论文本");
        // site是分享此内容的网站名称，仅在QQ空间使用
        oks.setSite("因味");
        // siteUrl是分享此内容的网站地址，仅在QQ空间使用
        oks.setSiteUrl("http://sharesdk.cn");
// 启动分享GUI
        oks.show(context);
    }
}
